import sys
import os
import datetime
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *
from pyqtutil import *
class Demo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        layout = QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop)
        
        toolBar=QToolBar()

        cb = QCheckBox('CalendarPopup')
        cb.stateChanged.connect(self.OnCalendarPopupChanged)
        toolBar.addWidget(cb)

        toolBar.addWidget(QLabel('Section:'))
        section =QComboBox()
        section.addItems(QDateTimeEdit.Section.__members__.keys())
        section.currentTextChanged.connect(self.OnSectionChanged)
        toolBar.addWidget(section)

        toolBar.addWidget(QLabel('DisplayFormat'))
        self.displayFormat =QLineEdit()
        self.displayFormat.editingFinished.connect(self.SetDisplayFormat)
        toolBar.addWidget(self.displayFormat)

        layout.addWidget(toolBar)
        
        self.dateEdit=QDateEdit()
        date= datetime.date.today()
        #self.timeEdit.setDateTime(QDate(date.year,date.month,date.day))
        self.dateEdit.setDate(PyQtUtil.QDate.fromPython(date))   
        layout.addWidget(self.dateEdit)

        self.setLayout(layout)
    def OnCalendarPopupChanged(self,state):
        self.dateEdit.setCalendarPopup(state)
        self.parent.OnShowMessage(f"Set CalendarPopup={state}")
    def OnSectionChanged(self,text):
        section = QDateTimeEdit.Section[text]
        self.dateEdit.setSelectedSection(section)        
        self.parent.OnShowMessage(f'Set SelectedSection: {section}')
    def SetDisplayFormat(self):
        format = self.displayFormat.text()
        self.dateEdit.setDisplayFormat(format)
        self.parent.OnShowMessage(f'Set DisplayFormat: {format}')
def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet